<?php

namespace App\Dao;
use App\Models\Topics;
use App\Models\Donates;
use App\Models\Payments;
use EasyWeChat\Factory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\DB;
/**
 * 主题活动
 *
 * @author Yh 20201201
 */
class TopicDao extends Topics
{
    public static function finish_check(){
        $unfinish_Topics =  Topics::where('status','!=',4)->get(['id','end_date','start_date','status','max_num','joins']);
        $unfinish_Topics->each(function ($topic) {
            self::check($topic);
        });
    }
    public static function check($topic){
        $star = strtotime($topic->start_date);
        $end = strtotime($topic->end_date);
        $now = strtotime(date('Y-m-d H:i:s'));
        $data['now'] =$now; 
        $data['end'] =$end;
        $data['star'] =$star;
        $data['rent'] =$now-$end;
        print_r($data);
        //修改未开始活动状态
        if(($star-$now)>0){
            $topic->status = 1;
            $topic->save();
            return ;
        }
        // 活动进行中
        if((($star-$now)<=0)&&(($end-$now)>=0)){
            if($topic->joins>=$topic->max_num){
                $topic->status=4;
                $topic->save();
                return ;
            }
            $topic->status = 2;
            $topic->save();
            return ;
        }
        if(($now-$end)>0){
            //判断是否达成最大开团数
            if($topic->joins>=$topic->max_num){
                $topic->status=4;
                $topic->save();
                
            }else{
                // 判断活动是否完成，未完成退款执行
                $topic->status=3;
                $topic->save();
                $donates = Donates::where('topic_id',$topic->id)->where('status',4)->get(['order_sn','id','total','out_sn']);
                $donates->each(function($donate){
                   self::refund($donate);
                });
            }
        }

    }
    public static function refund($donate){
        DB::transaction(function () use ($donate) {
            // 取得支付方式
            $payment_id = PAYMENT_ID_WXPAY;
            $payment = Payments::findOrFail($payment_id)->first();
            $payment_info =  unserialize(preg_replace_callback('#s:(\d+):"(.*?)";#s',function($match){return 's:'.strlen($match[2]).':"'.$match[2].'";';},$payment->payment_info));
            $wxapp = Factory::payment([
                'app_id' => $payment_info['wxAppId'],
                'mch_id' => $payment_info['wxMchId'],
                'key' => $payment_info['wxkey'],
                'cert_path' => $payment_info['cert_path'],
                'key_path' => $payment_info['key_path']
            ]);
            
            $result = $wxapp->refund->byOutTradeNumber($donate['order_sn'], $donate['out_sn'], intval($donate['total'] * 100), intval($donate['total'] * 100));
                
            // 记录同步通知日志
            $logObj = app('logext');
            $logObj->init('wxpay_refund');
            $logObj->logbuffer('reponse', $result);
            $logObj->logend();
            if ($result['return_code'] == 'SUCCESS' && $result['result_code'] == 'SUCCESS') {
                $donate->refund_time = date('Y-m-d H:i:s');
                $donate->status = 3;
                $donate->save();
                return true;
            }
              
        });
    }
}
